package com.jinly.ryder.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinly.ryder.manage.dao.AppInstanceMapper;
import com.jinly.ryder.manage.dao.entity.AppInstanceDO;
import com.jinly.ryder.manage.service.AppInstanceService;
import com.jinly.ryder.manage.service.mapping.AppInstanceMapping;
import com.jinly.ryder.manage.service.model.AppInstance;
import com.jinly.ryder.manage.service.model.DeployStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class AppInstanceServiceImpl implements AppInstanceService {

    private final AppInstanceMapping appInstanceMapping;
    private final AppInstanceMapper appInstanceMapper;

    @Override
    public Page<AppInstance> instancePage(Page page, AppInstance query) {
        Wrapper<AppInstanceDO> queryWrapper = new QueryWrapper<AppInstanceDO>().lambda()
                .eq(query.getAppName() != null, AppInstanceDO::getAppName, query.getAppName())
                .eq(query.getDeployGroupName() != null, AppInstanceDO::getDeployGroupName, query.getDeployGroupName())
                .eq(query.getEnvType() != null, AppInstanceDO::getEnvType, query.getEnvType())
                .like(query.getHost() != null, AppInstanceDO::getHost, query.getHost());
        return appInstanceMapping.toModelPage(appInstanceMapper.selectPage(page, queryWrapper));
    }

    @Override
    public List<AppInstance> list(String appName, String deployGroupName) {
        Wrapper<AppInstanceDO> queryWrapper = new QueryWrapper<AppInstanceDO>()
                .lambda()
                .eq(AppInstanceDO::getAppName, appName)
                .eq(deployGroupName != null, AppInstanceDO::getDeployGroupName, deployGroupName);
        return appInstanceMapping.toModelList(appInstanceMapper.selectList(queryWrapper));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(AppInstance appInstance) {
        if (appInstance.getId() == null) {
            appInstance.setDeployStatus(DeployStatus.NONE);
            log.info("新增={}", appInstance);
            appInstanceMapper.insert(appInstanceMapping.toEntity(appInstance));
        } else {
            log.info("更新={}", appInstance);
            appInstanceMapper.updateById(appInstanceMapping.toEntity(appInstance));
        }
    }

    @Override
    public AppInstance get(Integer appInstanceId) {
        return appInstanceMapping.toModel(appInstanceMapper.selectById(appInstanceId));
    }


    @Override
    public void updateDeployStatus(Integer instanceId, DeployStatus deployStatus) {
        AppInstanceDO appInstanceDO = appInstanceMapper.selectById(instanceId);
        if (appInstanceDO != null) {
            appInstanceDO.setDeployStatus(deployStatus);
            appInstanceMapper.updateById(appInstanceDO);
        }
    }


}
